package com.easpeed.user

import android.os.Bundle
import android.util.Log;
import com.bigyu.utils.Logger
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.ToastUtils
import com.easpeed.comon.utils.ApiHelper
import com.easpeed.comon.utils.CacheHelper
import com.easpeed.elevator.R
import com.easpeed.elevator.databinding.ActivityUserLoginBinding
import com.easpeed.comon.utils.CommonOperateUtils
import com.easpeed.comon.utils.NetHelper
import com.easpeed.comon.utils.isUser
import com.easpeed.elevator.ui.MainActivity
import com.gyf.immersionbar.ImmersionBar

class UserLoginActivity : UserBaseActivity() {

    var binding: ActivityUserLoginBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun initView() {
        ImmersionBar.with(this)
            .statusBarDarkFont(false) // 设置状态栏字体为深色
            .transparentStatusBar()  // 透明状态栏
            .fitsSystemWindows(false)
            .init()
        binding = DataBindingUtil.setContentView<ActivityUserLoginBinding>(this, R.layout.activity_user_login)


        val cacheName = CacheHelper.instance().getStringValue("loginName")
        val cachePwd = CacheHelper.instance().getStringValue("loginPwd")

        if (cacheName != null && cachePwd != null) {
            binding?.name?.setText(cacheName)
            binding?.pwd?.setText(cachePwd)
        }

        binding?.login?.setOnClickListener {

            netForLogin()
        }
    }

    private lateinit var netHelper: NetHelper
    private fun netForLogin() {
        if (!::netHelper.isInitialized) {
            netHelper = NetHelper(this)
        }
        netHelper.netForLogin(HashMap<String, Any>().apply {
            put("account", binding?.name?.text.toString())
            put("confirmPassword", binding?.pwd?.text.toString())
            put("password", binding?.pwd?.text.toString())
            put("platformType", "1")
        }) {
            if (it.code == 200) {
                CacheHelper.instance().apply {
                    this.save("loginName", binding?.name?.text.toString())
                    this.save("loginPwd", binding?.pwd?.text.toString())
                    this.save("token", it.data?.token)
                    this.save("userId", it.data?.sysUser?.id?.toString())
                    this.save("loginCache", true)
                    this.save("isLogin", true)
                }
                editor.commit()
                if (isUser()) {
                    CommonOperateUtils.moveTo(this, UserMainActivity::class.java)
                } else {
                    CommonOperateUtils.moveTo(this, MainActivity::class.java)
                }
                finish()
            } else {
                ToastUtils.showShort("${it.message}")
            }
        }

    }
}